package com.cx.purchase.subclass.controller;

import com.cx.purchase.config.ResultVO;
import com.cx.purchase.config.ResultVOBuilder;
import com.cx.purchase.config.Status;
import com.cx.purchase.subclass.mapper.NewJapSubClassMapper;
import com.cx.purchase.subclass.mapper.SubClassJpaMapper;
import com.cx.purchase.subclass.mapper.SubclassMapper;
import com.cx.purchase.subclass.model.*;

import com.cx.purchase.subclass.service.PurchaseServiceImpl;
import com.cx.purchase.util.AllrecordsVo;

import com.querydsl.core.types.dsl.BooleanExpression;
import com.querydsl.core.types.dsl.Expressions;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Sort;
import org.springframework.util.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@CrossOrigin //支持跨域请求
public class PurchaseController {

    @Autowired
    private SubClassJpaMapper subClassJpaMapper;


    //添加
    @GetMapping("/Create/JPA")
    public JPASubClass add(){
        JPASubClass jpaSubClass = new JPASubClass();
        jpaSubClass.setCode("编码1");
        jpaSubClass.setName("名称1");
        jpaSubClass.setStatus(1);
        JPASubClass save = subClassJpaMapper.save(jpaSubClass);
        return save;
    }
    //修改
    @GetMapping("/Update/JPA")
    public JPASubClass update(){
        JPASubClass jpaSubClass = new JPASubClass();
        jpaSubClass.setId(2);
        jpaSubClass.setCode("编码");
        jpaSubClass.setName("名称");
        jpaSubClass.setStatus(0);
        JPASubClass save = subClassJpaMapper.save(jpaSubClass);
        return save;
    }
    //全查
    @GetMapping("/Query/JPA")
    public List<JPASubClass> query(){
        List<JPASubClass> all = subClassJpaMapper.findAll();
        return all;
    }
    @Autowired
    private NewJapSubClassMapper newJapSubClassMapper;

    /*
     * 备件小类增加
     * */
    @PostMapping("/subClass/queryPage")
    public ResultVO<AllrecordsVo> queryPage(@RequestBody @Valid NewJpaSubclass subclass, BindingResult bd){
        //索引 和 每页大小   排序   条件
        BooleanExpression booleanExpression = Expressions.asBoolean(true).isTrue();
        if (!StringUtils.isEmpty(subclass.getName())){
            booleanExpression =  booleanExpression.and(QNewJpaSubclass.newJpaSubclass.name.eq(subclass.getName()));
        }
        if (!StringUtils.isEmpty(subclass.getCode())){
            booleanExpression =  booleanExpression.and(QNewJpaSubclass.newJpaSubclass.code.eq(subclass.getCode()));
        }
        //1.查询条件，2.索引和每页的大小，3.排序根据某个字段进行排序
        Sort id = Sort.by(Sort.Direction.DESC, "createTime");
        Page<NewJpaSubclass> plantPage = newJapSubClassMapper.findAll(booleanExpression,  PageRequest.of(subclass.getOffset(), subclass.getPageSize(),id));
        for (NewJpaSubclass s:plantPage) {
            s.setStatusName(s.getStatus()==true?"启用":"禁用");
        }
        AllrecordsVo allRecords = new AllrecordsVo();
        allRecords.setDataList(plantPage.getContent());
        allRecords.setTotalNumber(plantPage.getNumber());
        allRecords.setPageIndex(subclass.getPageIndex());
        allRecords.setPageSize(subclass.getPageSize());
        allRecords.resetTotalNumber(plantPage.getNumber());


        return ResultVOBuilder.success(allRecords);

    }
    /*
    * 备件小类增加
    * */
    @PostMapping("/subClass/add")
    public ResultVO<Subclass> add(@RequestBody @Valid NewJpaSubclass subclass, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        String name = "张三";
        Date date = new Date();
        subclass.setCreateName(name);
        subclass.setCreateTime(date);
        subclass.setUpdateTime(date);
        subclass.setUpdateName(name);

        newJapSubClassMapper.save(subclass);

        return ResultVOBuilder.success();
    }
    /*
     * 备件小类修改
     * */
    @PostMapping("/subClass/update")
    public ResultVO<Subclass> update(@RequestBody @Valid NewJpaSubclass subclass, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        String name = "王五";
        Date date = new Date();
        subclass.setUpdateTime(date);
        subclass.setUpdateName(name);
        subclass.setId(subclass.getId());
        subclass.setName(subclass.getName());
        subclass.setRemark(subclass.getRemark());
        subclass.setStatus(subclass.getStatus());


        newJapSubClassMapper.save(subclass);

        return ResultVOBuilder.success();
    }
    /*
     * 备件小类修改状态
     * */
    @PostMapping("/subClass/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid NewJpaSubclass subclass, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        subclass.setId(subclass.getId());
        subclass.setStatus(subclass.getStatus());

        newJapSubClassMapper.save(subclass);

        return ResultVOBuilder.success();
    }



    @Autowired
    private SubclassMapper subclassMapper;

    @Autowired
    private PurchaseServiceImpl purchaseServiceImpl;




    @PostMapping("/metadata/getPullDownList")
    public ResultVO getPullDownList(){
        ArrayList<StatusVo> list = new ArrayList<>();
        StatusVo statusVo = new StatusVo();
        statusVo.setLabel("启用");
        statusVo.setValue(1);
        StatusVo statusVo1 = new StatusVo();
        statusVo1.setValue(0);
        statusVo1.setLabel("禁用");
        list.add(statusVo);
        list.add(statusVo1);

        return ResultVOBuilder.success(list);
    }

//    @PostMapping("/subClass/queryPage")
//    public ResultVO queryPage(@RequestBody SubClassSelectVo subClassSelectVo){
//        //接受到了前端穿过来的参数
//        //计算索引
//
//        return purchaseServiceImpl.queryPage(subClassSelectVo);
//
//    }

    /*@PostMapping("/subClass/add")
    public ResultVO add(@RequestBody @Valid SubClassAddVo subClassAddVo, BindingResult bd){
        if(bd.hasErrors()){
            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }

        return purchaseServiceImpl.add(subClassAddVo);
    }*/

   /* @PostMapping("/subClass/update")
    public ResultVO update(@RequestBody @Valid SubClassUpdateVo subClassUpdateVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.update(subClassUpdateVo);
    }*/
   /* @PostMapping("/subClass/updateStatus")
    public ResultVO updateStatus(@RequestBody @Valid SubClassUpdateStatusVo subClassUpdateStatusVo, BindingResult bd){
        if(bd.hasErrors()){

            return ResultVOBuilder.error(bd.getFieldError().getDefaultMessage());
        }
        return  purchaseServiceImpl.updateStatus(subClassUpdateStatusVo);
    }*/


    @GetMapping("/planClass/queryById")
    public ResultVO queryById( Integer id ){
        if(id==null){
            ResultVOBuilder.error("Id不能为空");
        }
        return   purchaseServiceImpl.queryById(id);
    }

    @GetMapping("/subclass/delete")
    public String delete(Integer id){
        int i = subclassMapper.delete(id);
        return  i > 0 ?"删除成功":"删除失败";
    }

}
